package com.yehongyu.mytools.database;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

//import com.sinosoft.database.DBConnectionPool;

public class PrintNeedString {
	
	private Connection conms=null;
	private Statement stmtn=null;
	private Connection conor=null;

	public PrintNeedString() {
		super();
		// TODO Auto-generated constructor stub
	}
	/**
	 * 打开连接，这里用JDBC同时打开两个连接。
	 */
	public boolean openConnection(){
		try {
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
					.newInstance();
			String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=egov";
			// egov为数据库
			String user = "sa";
			String password = "sa";
			conms = DriverManager.getConnection(url, user, password);

			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
			String url2 = "jdbc:oracle:thin:@192.168.70.243:1521:egov";
			// egov为数据库的SID
			String user2 = "eos";
			String password2 = "eos";
			conor = DriverManager.getConnection(url2, user2, password2);
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (SQLException e1) {
			e1.printStackTrace();
		} 

		// conms = DBConnectionPool.getConnection("mssql");
		if (conms == null) {
			System.out.println(":-( mssql连接失败");
			return false;
		} else
			System.out.println(":-) mssql连接成功");

		// conor = DBConnectionPool.getConnection("oracle");
		if (conor == null) {
			System.out.println(":-( oracle连接失败");
			return false;
		} else
			System.out.println(":-) oracle连接成功");
		try {
			stmtn = conor.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	/**
	 * 关闭连接
	 */
	public boolean closeConnection(){
		try {
			stmtn.close();
			if (conms != null) {
				conms.close();
			}
			if (conor != null) {
				conor.close();
			}
			return true;
		} catch (SQLException e1) {
			System.out.println("出现异常" + e1.toString());
			// e1.printStackTrace();
			return false;
		}
	}

	public void executeSql(){
		this.openConnection();
		String exesql = "delete from eg_subletter t Where length(sublet_id)<15";

		int i;
		try {
			i = stmtn.executeUpdate(exesql);
			System.out.println("删除" + i + "行");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.closeConnection();	
	}
	public void queryOrgID(){
		this.openConnection();
//		Connection conms = DBConnectionPool.getConnection("mssql");
		if (conms==null){
			System.out.println(":-( mssql连接失败");
		}else System.out.println(":-) mssql连接成功");
		
//		Connection conor = DBConnectionPool.getConnection("oracle");
		if (conor==null){
			System.out.println(":-( oracle连接失败");
		}else System.out.println(":-) oracle连接成功");
		String queryString = "select * from egov.sys_org;";
		try {
			Statement stmt = conms.createStatement();
			ResultSet rs = stmt.executeQuery(queryString);
			Statement stmtor = conor.createStatement();
			ResultSet rsor = null;
			int maxNo = 0;
			System.out.println("===============数据导出开始=================");
			while (rs.next()) {
				int oid=rs.getInt(1);
				String oname=rs.getString(3);
				System.out.print("旧库部门ID（"+oid+"）————部门名称（"+oname+"）");
				rsor= stmtor.executeQuery("select * from eosorg_t_organization where orgname='"+oname+"'");
				if (rsor.next()){
					System.out.println("————新库部门ID（"+rsor.getInt(1)+"）");
				}else{
					System.out.println("——————————————————————————————————————————————此部门名称新库中不存在");
				}
				maxNo++;
				//if (maxNo >= 5)
				//	break;
			}
			rsor.close();
			stmtor.close();
			rs.close();
			stmt.close();
			System.out.println("===================数据导出完毕，共"+maxNo+"条======================");
			
		} catch (SQLException e) {
			System.out.println("出现异常" + e.toString());
			// e.printStackTrace();
		}
		this.closeConnection();
	}
	public void queryOrgID2(){
//		Connection conms = DBConnectionPool.getConnection("mssql");
//		if (conms==null){
//			System.out.println(":-( mssql连接失败");
//		}else System.out.println(":-) mssql连接成功");
//		
//		Connection conor = DBConnectionPool.getConnection("oracle");
//		if (conor==null){
//			System.out.println(":-( oracle连接失败");
//		}else System.out.println(":-) oracle连接成功");
		this.openConnection();
		
		String queryString = "select * from eosorg_t_organization";
		try {
			Statement stmt = conms.createStatement();
			ResultSet rs = null;
			Statement stmtor = conor.createStatement();
			
			ResultSet rsor = stmtor.executeQuery(queryString);
			int maxNo = 0;
			System.out.println("===============数据导出开始=================");
			
			while (rsor.next()) {
				int oid=rsor.getInt(1);
				String oname=rsor.getString(3);
				System.out.print("新库部门ID（"+oid+"）——部门名称（"+oname+"）");
				rs= stmt.executeQuery("select * from egov.sys_org where org_sCname='"+oname+"';");
				if (rs.next()){
					int x=rs.getInt(1);
					//System.out.println("——旧库部门ID（"+x+"）");
					if (oid!=x){
						System.out.println("——旧库部门ID（"+x+"）----------------------------不同");
					}else{
						System.out.println("——旧库部门ID（"+x+"）----相同");
					}
				}else{
					System.out.println("——————————————————————————————————————————————————此部门名称旧库中不存在");
				}
				maxNo++;
				//if (maxNo >= 5)
				//	break;
			}
			rsor.close();
			stmtor.close();
			rs.close();
			stmt.close();
			System.out.println("===================数据导出完毕，共"+maxNo+"条======================");
			
		} catch (SQLException e) {
			System.out.println("出现异常" + e.toString());
			// e.printStackTrace();
		}
		this.closeConnection();
	}
	  /**
	   * 判断是否是GBK编码
	   * @param tStr
	   * @return
	   */
	  public static boolean isGBKString(String tStr)
	  {
	    int tlength=tStr.length();
//	    Integer t=new Integer(0);
	    int t1=0;
	    for(int i=0;i<tlength;i++)
	    {
	      t1=Integer.parseInt(Integer.toOctalString(tStr.charAt(i)));
	      if(t1>511) return true;
	    }
	    return false;
	  }
	  /**
	   * 判断是否是Unicode编码
	   * @param tStr
	   * @return
	   */
	  public static boolean isUnicodeString(String tStr)
	  {
	    int tlength=tStr.length();
//	    Integer t=new Integer(0);
	    int t1=0;
	    for(int i=0;i<tlength;i++)
	    {
	      t1=Integer.parseInt(Integer.toOctalString(tStr.charAt(i)));
	      if(t1>511) return false;
	    }
	    return true;
	  }
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
//		String tStr = "";
//		for (int i = 1 ;i<=89;i++){
//			tStr =tStr +  "String col"+i+" = tLetN.getLET_ID();\n";  
//			
//		}
//		System.out.print(tStr);
		Date td=new Date(1000*60*60*24*24+1000*60*60*20+1000*60*31+1000*23+647);
//		double times=td.getTime();
//		System.out.println(td);
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
		String dateFormat=sdf.format(td);
		System.out.println(dateFormat);

		//PrintNeedString pns= new PrintNeedString();
//		pns.queryOrgID2();
		//pns.executeSql();
//		System.out.println("?±?°?°?°????".length());
//		System.out.println("100094".length());
//		System.out.println("１０００９４".toLowerCase().length());
//		String a = "";
//		System.out.println(PrintNeedString.isGBKString(a));
//		System.out.println(PrintNeedString.isUnicodeString(a));
//		System.out.println(a);
//		System.out.println(a.length());
//		a = new String(a.getBytes("gb2312"),"iso-8859-1");
//		System.out.println(PrintNeedString.isGBKString(a));
//		System.out.println(a);
//		System.out.println(a.length());
//		a = new String(a.getBytes("iso-8859-1"),"gb2312");
//		System.out.println(PrintNeedString.isGBKString(a));
//		System.out.println(a);
//		System.out.println(a.length());
//		int xx = Integer.parseInt("2501328800".substring("2501328800".length()-2,"2501328800".length()))+1;
//		String x="";
//		if (xx<10){
//			x = "0" + xx;
//		}else x = ""+xx;
//		System.out.println("25013288" + x );
//	    Calendar cal  = Calendar.getInstance();        
//	    String date_now=cal.get(cal.YEAR)+"年"+(cal.get(cal.MONTH)+1)+"月"+cal.get(cal.DATE)+"日 ";    
//	    String time_now=cal.get(cal.HOUR_OF_DAY)+"时"+cal.get(cal.MINUTE)+"分"+cal.get(cal.SECOND)+"秒";        
//	    System.out.println(date_now);    
//	    System.out.println(time_now);
//	    System.out.println(cal.getTime().toLocaleString());
	}

}
